package com.le.controllers;

import com.baomidou.mybatisplus.extension.api.R;
import com.le.bean.Reply;
import com.le.bean.Topic;
import com.le.biz.ReplyBiz;
import com.le.biz.TopicBiz;
import com.le.web.model.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("reply") // http://localhost:port/reply
@Slf4j
@Api(tags = "回复")
public class ReplyController {
    @Autowired
    private ReplyBiz replyBiz;

    @RequestMapping(value = "findByPage", method = {RequestMethod.GET})
    @ApiOperation(value = "分页查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageno", value = "页号", required = true),
            @ApiImplicitParam(name = "pagesize", value = "每页记录数", required = true),
            @ApiImplicitParam(name = "topicid", value = "主题/帖子编号", required = true)
    })
    public Map<String, Object> findByPage(@RequestParam int pageno, @RequestParam int pagesize, @RequestParam Integer topicid){
        Map<String, Object> map = new HashMap<>(); // 返回json字符串

        PageBean<Reply> page = null;
        try {
            page = this.replyBiz.findByPage(pageno, pagesize, topicid);
        } catch (Exception e){
            map.put("code", 0);
            map.put("msg", e.getCause());
            e.printStackTrace();
            return map;
        }
        map.put("code", 1);
        map.put("data", page);
        return map;
    }

    @RequestMapping(value = "deleteReplyById", method = {RequestMethod.GET})
    @ApiOperation(value = "根据replyid删除reply的操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "replyid", value = "回复编号", required = true)
    })
    public Map<String, Object> deleteReplyById(@RequestParam Integer replyid) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        this.replyBiz.deleteReplyById(replyid);
        map.put("code", 1);
        return map;
    }

    @RequestMapping(value = "addReply", method = {RequestMethod.POST})
    @ApiOperation(value = "发布回复的操作")
    public Map<String, Object> addReply(Reply reply) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        this.replyBiz.addReply(reply);
        map.put("code", 1);
        return map;
    }

    @RequestMapping(value = "updateReply", method = {RequestMethod.POST})
    @ApiOperation(value = "修改回复的操作")
    public Map<String, Object> updateReply(Reply reply) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        this.replyBiz.updateReply(reply);
        map.put("code", 1);
        return map;
    }

    @RequestMapping(value = "isReplyId", method = {RequestMethod.GET})
    @ApiOperation(value = "判断replyid是否存在操作")
    public Map<String, Object> isReplyId(Integer replyid) {
        Map<String, Object> map = new HashMap<>(); // 返回json字符串
        int code = this.replyBiz.isReplyId(replyid);
        map.put("code", code);
        return map;
    }
}
